package com.monkey.demo.excel;

import com.monkey.demo.dao.SysUserDao;
import com.monkey.demo.entity.SysUser;
import com.monkey.demo.service.SysUserService;
import com.monkey.tool.excel.BaseEasyExcelExport;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * User: zhangqi
 * Date: 2024/7/28
 *
 * @Description:通过工具类实现导出数据
 */
@Component
public class SysUserExport extends BaseEasyExcelExport {

    @Resource
    private SysUserDao sysUserDao;

    /**
     * 执行数据导出数据导出
     */
    public void exportWithBigData(String fileName, Map<String, Object> conditions) {
        this.exportExcel(fileName, conditions);
    }

    /**
     * 定义导出的表头
     * @return
     */
    @Override
    protected List<List<String>> getExcelHead() {
        List<List<String>> head = new ArrayList<>();
        head.add(Collections.singletonList("用户编号"));
        head.add(Collections.singletonList("用户姓名"));
        return head;
    }

    /**
     * 每个sheet页的数据条数
     * @return
     */
    @Override
    protected Long eachSheetTotalCount() {
        return 10L;
    }

    /**
     *每次写入sheet的总数
     * @return
     */
    @Override
    protected Long eachTimesWriteSheetTotalCount() {
        return 2000L;
    }

    /**
     * 构建数据-根据条件查询数据
     * @param resultList
     * @param queryCondition
     * @param pageNo
     * @param pageSize
     */
    @Override
    protected void buildDataList(List resultList, Map queryCondition, Long pageNo, Long pageSize) {
        //根据条件查询数据
        List<SysUser> sysUsers = sysUserDao.selectList();
        //构建每一行的数据
        if (CollectionUtils.isNotEmpty(sysUsers)) {
            sysUsers.forEach(sysUser -> {
                resultList.add(Arrays.asList(sysUser.getId().toString(), sysUser.getUserName()));
            });
        }
    }

    /**
     *
     * @param conditions
     * @return
     */
    @Override
    protected Long dataTotalCount(Map conditions) {
        SysUser sysUser = new SysUser();
        long count = sysUserDao.count(sysUser);
        return count;
    }
}
